var app = angular.module('AppControllers',[]);

app.controller('IndexController',function($scope){
	
});

app.controller('RegistrationController', function($scope, $location, RegistrationService){
	$scope.user = {};
	$scope.user.usLogin='';
	$scope.user.usPassword='';
	$scope.user.usName='';
	$scope.user.usLastName='';
	$scope.user.usStreet='';
	$scope.user.usHouseNumber='';
	$scope.user.usZipCode='';
	$scope.user.locality='';
	$scope.user.usEmail='';
	$scope.value = 'false';
	var required = function(){
		if($scope.user.usLogin=='' ||
	$scope.user.usPassword==''||
	$scope.user.usName==''||
	$scope.user.usLastName==''||
	$scope.user.usStreet==''||
	$scope.user.usHouseNumber==''||
	$scope.user.usZipCode==''||
	$scope.user.locality==''||
	$scope.user.usEmail==''){
			$scope.value = 'true';
		}
		else{
			$scope.value = 'false';
		}
	};
	
	$scope.getValue = function(){
		required();
		return $scope.value;
	};
	
	$scope.sendData = function(){
			var value = RegistrationService.newUser($scope.user);
			if(value =='duplicate_entry'){
				ErrorService.setValue(value);
				$location.path('/registration/error');
			}
			else{
				$location.path('/registration/success');
			}
	};
	$scope.data ='';
	$scope.exist = function(){
	RegistrationService.ifExist($scope.user.usLogin).then(function(data){
			$scope.data = data.content;
			if(data.flag == 'true'){
				$scope.user.usLogin = '';
			}
			
		});
	
	
		
	};
		
});

app.controller('ErrorController',function($scope, ErrorService){
	$scope.errorMessage = ErrorService.getErrorData();
});

app.controller('ArticleController', function($scope, $http, $route, $routeParams, CommentService){
	var id = $routeParams.articleId;
	$scope.user = {};
	$scope.login = '';
	$scope.content = '';
	$scope.id = '';
	$http.post('article/getData', id).success(function(data){
		$scope.title = data.title;
		$scope.content = data.content;
		$scope.date = data.date;
		$scope.author = data.author;
	});
	
	$http.post('article/getComments',id).success(function(data){
		$scope.comments = data;
	});
	$scope.reloadPage = function(){
		$route.reload();
	};
	
	$scope.addComment = function(){
		$scope.user.id = id;
		CommentService.addComment($scope.user);
		$scope.reloadPage();
	};
	
});

app.controller('LoginController', function($scope, $routeParams){
	var loginFail = $routeParams.flag;
	if(loginFail == 'fail'){
		$scope.content = "Nieprawidłowy login lub hasło";
		$scope.color = "red";
	}else if(loginFail == 'success'){
		$scope.content = "Zostałeś zalogowany pomyślnie";
		$scope.color = "green";
	}else if(loginFail == 'logout'){
		$scope.content = "Zostałeś wylogowany pomyślnie";
		$scope.color = "green";
	}	
});

app.controller('AdminController', function($scope, $routeParams){
	
});